package org.luckyjourney.controller.consultants;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.luckyjourney.service.consultants.SmsService;
import org.luckyjourney.util.RandomCode;
import org.luckyjourney.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/luckyjourney/sms")
@Slf4j
//登录接口
@Api(tags = "短信服务")
public class SmsController {

    @Autowired
    SmsService smsService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;//防止乱码

    @PostMapping("/sendMessage")
    @ApiOperation("短信服务")
    public Result SendSms(String phone) throws Exception {
        String code1 = RandomCode.genCode();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", code1);
        String sendSmsResponse = smsService.sendSms(phone, map);
        System.out.println(sendSmsResponse);
        if ("OK".equals(sendSmsResponse)) {
            //如果发送 成功 存取到redis 一分钟的有效时间
            redisTemplate.opsForValue().set(phone,code1,30, TimeUnit.MINUTES);
            return Result.success("发送成功");
        }

        return Result.error("发送失败");
    }



}
